<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="keyWord" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="onSerach" />
        <!-- 树形组件 -->
        <el-tree ref="treeRef" node-key="id" highlight-current :data="treeData" :props="{ label: 'name' }" default-expand-all :expand-on-click-node="false" @current-change="changeCurrent" />
      </div>

      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>

          <el-button size="mini" @click="isShow=true">excel导入</el-button>

          <el-button size="mini" @click="onExport">excel导出</el-button>

        </el-row>

        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" />
              <div v-else class="username">
                {{ row.username.charAt(0) }}
              </div>

            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template #default="{row}">
              <div>
                {{ row.formOfEmployment === 1 ? '正式' : '非正式' }}
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template #default="{row}">
              <el-button v-xxx="'show'" size="mini" type="text" @click="$router.push('/employee/detail/'+row.id)">查看</el-button>

              <el-button v-xxx="'assign'" size="mini" type="text" @click="onOpen(row.id)">角色</el-button>
              <el-popconfirm title="确认删除该行数据吗？" @onConfirm="onDelById(row.id)">
                <template #reference>

                  <el-button v-xxx="'del'" style="margin-left:10px" size="mini" type="text">删除</el-button>
                </template>

              </el-popconfirm>

            </template>

          </el-table-column>

        </el-table>

        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            layout="total,prev, pager, next"
            :total="total"
            :current-page="queryParams.page"
            :page-size="queryParams.pagesize"
            @current-change="changePage"
          />
        </el-row>
        <ImportExcel :show-excel-dialog.sync="isShow" />
      <!-- 分页 -->
      </div>

    </div>
    <el-dialog :visible.sync="showRoleDialog" title="分配角色">
      <!-- 弹层内容 -->
      <!-- checkbox -->
      <el-checkbox-group v-model="roleIds">
        <!-- 放置n个的checkbox  要执行checkbox的存储值 item.id-->
        <el-checkbox
          v-for="item in roleList"
          :key="item.id"
          :label="item.id"
        >{{ item.name }}</el-checkbox>

      </el-checkbox-group>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="mini" @click="onUpdate">确定</el-button>

          <el-button size="mini" @click="showRoleDialog = false">取消</el-button>

        </el-col>

      </el-row>

    </el-dialog>

  </div>

</template>

<script>
import { getDepartmentAPI } from '@/api/department'
import { assignRoleAPI, delEmployeeAPI, exportAPI, getEmployeeDetailAPI, getEmployeeListAPI } from '@/api/employee'
import { transListToTreeData } from '@/utils'
import { saveAs } from 'file-saver'
import ImportExcel from './import-excel.vue'
import { getEnableRoleListAPI } from '@/api/permission'

export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {
      treeData: [],
      depId: null,
      list: [],
      queryParams: {
        departmentId: null,
        page: 1, // 当前页码
        pagesize: 10
      },
      total: 0, // 记录员工的总数
      keyWord: '',
      isShow: false,
      showRoleDialog: false, // 用来控制角色弹层的显示
      roleList: [], // 接收角色列表
      roleIds: [] // 用来双向绑定数据的
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    async loadData() {
      const res = await getDepartmentAPI()
      console.log(res)
      const xxx = transListToTreeData(res, 0)
      this.treeData = xxx
      this.depId = xxx[0].id
      this.loadEmployee()
      this.$nextTick(() => {
        this.$refs.treeRef.setCurrentKey(xxx[0].id)
      })
    },
    changeCurrent(data) {
      this.depId = data.id
      this.loadEmployee()
    },
    async loadEmployee() {
      const { rows, total } = await getEmployeeListAPI({ keyword: this.keyWord, departmentId: this.depId, page: this.queryParams.page })
      this.list = rows
      // 2. 保存total
      this.total = total
    },
    changePage(current) {
      this.queryParams.page = current
      this.loadEmployee()
    },
    onSerach(value) {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.loadEmployee()
      }, 500)
    },
    async onExport() {
      const res = await exportAPI()
      saveAs(res, '员工信息.xlsx')
    },
    async onDelById(id) {
      await delEmployeeAPI(id)
      if (this.list.length === 1 && this.queryParams.page > 1) {
        this.queryParams.page--
      }
      this.loadEmployee()
      this.$message.success('删除成功')
    },
    async onOpen(id) {
      this.roleList = await getEnableRoleListAPI()
      const res = await getEmployeeDetailAPI(id)
      this.showRoleDialog = true
      this.roleIds = res.roleIds
      this.xxxid = id
    },
    async onUpdate() {
      await assignRoleAPI({ roleIds: this.roleIds, id: this.xxxid })
      this.$message.success('分配成功')
      this.showRoleDialog = false
    }
  }
}
</script>

    <style lang="scss" scoped>
    .app-container {
    background: #fff;
    display: flex;
    .left {
      width: 280px;
      padding: 20px;
      border-right: 1px solid #eaeef4;
    }
    .right {
      flex: 1;
      padding: 20px;
      .opeate-tools {
        margin:10px ;
      }
      .username {
        height: 30px;
        width: 30px;
        line-height: 30px;
        text-align: center;
        border-radius: 50%;
        color: #fff;
        background: #04C9BE;
        font-size: 12px;
        display:inline-block;
      }
    }
  }

</style>
